package com.e3.basdoc.goodscls.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.goodscls.client.GoodsClsHelper;
import com.e3.basdoc.goodscls.client.GoodsClsRPCService;
import com.e3.basdoc.goodscls.model.GoodsClsModel;
import com.e3.basdoc.goodscls.model.GoodsClsQC;
import com.e3.core.base.ServiceException;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class GoodsClsRPCServiceImpl extends GwtRPCAction implements	GoodsClsRPCService {

	/**
	 * 
	 */
	public List<GoodsClsModel> getGoodsClsChildren(final GoodsClsModel goodsClsModel) throws WebException {
		return new GwtRPCExecutor<List<GoodsClsModel>>() {
			@Override
			public List<GoodsClsModel> execute() throws ServiceException, Exception {
				List<GoodsClsModel> models = new ArrayList<GoodsClsModel>();
				if(goodsClsModel == null){
					GoodsClsModel model = GoodsClsHelper.INSTANCE.createRootModel();
					models.add(model);
					return models;
				}
				
				GoodsClsModel qModel = goodsClsModel;
				if(GoodsClsHelper.INSTANCE.isRootModel(qModel)){
					qModel = null;
				}
				
				models = GoodsClsServiceClient.getGoodsClsQueryService().getGoodsClsChildren(qModel);
				return models;
			}
		}.doExecute();
	}

	public GoodsClsModel newGoodsCls(final GoodsClsModel goodsClsModel) throws WebException {
		return new GwtRPCExecutor<GoodsClsModel>() {

			@Override
			public GoodsClsModel execute() throws ServiceException, Exception {
				return GoodsClsServiceClient.getGoodsClsService().newGoodsCls(goodsClsModel);
			}

		}.doExecute();
	}

	public GoodsClsModel modifyGoodsCls(final GoodsClsModel goodsClsModel) throws WebException {
		return new GwtRPCExecutor<GoodsClsModel>() {

			@Override
			public GoodsClsModel execute() throws ServiceException, Exception {
				return GoodsClsServiceClient.getGoodsClsService().modifyGoodsCls(goodsClsModel);
			}

		}.doExecute();
	}

	public List<GoodsClsModel> getGoodsClsByCondition(final GoodsClsQC goodsClsQC) throws WebException {
		return new GwtRPCExecutor<List<GoodsClsModel>>() {
			@Override
			public List<GoodsClsModel> execute() throws ServiceException, Exception {
				List<GoodsClsModel> goodsClss = GoodsClsServiceClient.getGoodsClsQueryService().getGoodsClsByCondition(goodsClsQC);
				if (goodsClss == null || goodsClss.isEmpty()) {
					return goodsClss;
				}
				fillParentGoodsCls(goodsClss);
				return goodsClss;
			}

		}.doExecute();
	}

	protected void fillParentGoodsCls(List<GoodsClsModel> goodsClss) throws Exception{
		for (GoodsClsModel goodsClsModel : goodsClss) {
			if(goodsClsModel.getParentModel()!=null){
				continue;
			}
			long id = goodsClsModel.getId();
			if(id == 0){
				continue;
			}
			GoodsClsModel pModel = GoodsClsServiceClient.getGoodsClsQueryService().getGoodsClsById(id);
			goodsClsModel.setParentModel(pModel);
		}
	}

	public void deleteGoodsClses(final List<GoodsClsModel> goodsClss) throws WebException {
		new GwtRPCVoidExecutor() {

			@Override
			public void execute() throws ServiceException, Exception {
				if(goodsClss == null||goodsClss.isEmpty()){
					return;
				}
				List<Long> ids = new ArrayList<Long>(goodsClss.size());
				for (GoodsClsModel goodsClsModel : goodsClss) {
					long id = goodsClsModel.getId();
					ids.add(id);
				}
				GoodsClsServiceClient.getGoodsClsService().deleteGoodsClsByIds(ids);
			}

		}.doExecute();
	}
}
